﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using BZV.Core.Net;

namespace BZV.Foundation.Business
{
    public enum QueryState
    {
        EN_BoundsTime,
        EN_OpenWaitTime,
        EN_CountDownTime,
        EN_DelayTime
    }

    // 获取当前的开奖号码，以及当前时间处于什么状态
    public class QueryCode
    {
#region 成员变量
        // 当前的开奖时间和期号
        private StartTime m_startTime;
        private string m_strID;
        private QueryState m_QueryState = new QueryState();

        // 事件定义
        public EventHandler OnIntoWaite;                // 进入开奖等待阶段
        public EventHandler OnIntoCountDown;            // 进入倒计时阶段
        public EventHandler OnIntoGetTime;              // 进入到最后的获取阶段。
        public EventHandler OnOpen;                     // 开奖

        // 定时器
        System.Timers.Timer m_TickTimer;
#endregion

#region 属性
        // 开奖时间
        public StartTime startTime
        {
            get { return m_startTime; }
        }

        // 开奖状态
        public QueryState State
        {
            get { return m_QueryState; }
        }
#endregion

        /// <summary>
        /// 构造函数，创建当前的时间
        /// </summary>
        /// <param name="strID"></param>
        public QueryCode(string strID)
        {
            // 获取当前的开始时间
            m_startTime = Query.QueryStartTime(strID);
            m_strID = strID;
        }

        public void Start()
        {
            InitializeState();

            // 离线的时候不查询
            bool bOnLine = NetHelper.CheckOnLine();
            if (bOnLine)
            {
                SetTimer();
            }
        }

        /// <summary>
        /// 初始化当前状态
        /// </summary>
        private void InitializeState()
        {
            m_startTime = Query.QueryStartTime(m_strID);
            if (m_startTime == null)
            {
                return;
            }

            // 初始化变量

            // 投注时间
            if (DateTime.Now < m_startTime.FsEndTime)
            {
                m_QueryState = QueryState.EN_BoundsTime;
            }
            else if(DateTime.Now < m_startTime.EndTime)
            {
                m_QueryState = QueryState.EN_OpenWaitTime;
            }
            else if (DateTime.Now < m_startTime.EndTime.AddSeconds(40))
            {
                m_QueryState = QueryState.EN_CountDownTime;
                
            }
            else if (DateTime.Now < m_startTime.EndTime.AddSeconds(45))
            {
                m_QueryState = QueryState.EN_DelayTime;
            }
        }

        private void SetTimer()
        {
            if (m_TickTimer == null)
            {
                m_TickTimer = new System.Timers.Timer();
            }

            // 每个一段时间出发一次
            m_TickTimer.AutoReset = true;
            m_TickTimer.Interval = 1000;
            m_TickTimer.Enabled = true;
            m_TickTimer.Elapsed += new System.Timers.ElapsedEventHandler(Timers_Timer_Elapsed);
            m_TickTimer.Start();
        }

        /// <summary>
        /// 节拍器触发方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Timers_Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            // 设置触发事件
            if (Math.Abs((DateTime.Now - m_startTime.FsEndTime).TotalMilliseconds) <= 500)
            {
                new Thread((ThreadStart)delegate
                {
                    if (OnIntoWaite != null)
                    {
                        OnIntoWaite(null, null);
                    }
                    
                }
                ).Start();
            }
            else if (Math.Abs((DateTime.Now - m_startTime.EndTime).TotalMilliseconds) <= 500)
            {
                
                new Thread((ThreadStart)delegate
                {
                    if (OnIntoCountDown != null)
                    {
                        OnIntoCountDown(null, null);
                    }
                    
                }
                ).Start();
            }
            else if (Math.Abs((DateTime.Now - m_startTime.EndTime.AddSeconds(40)).TotalMilliseconds) <= 500)
            {
                
                new Thread((ThreadStart)delegate
                {
                    if (OnIntoGetTime != null)
                    {
                        OnIntoGetTime(null, null);
                    }
                    
                }
                ).Start();

            }
                // 6秒刷新来实时获取开奖信息
            else if ((m_startTime.EndTime.AddSeconds(45) - DateTime.Now).TotalMilliseconds <= 5000)
            {
                // 获取是否开奖
                Lottery lottery = Query.QueryLottery(m_strID, startTime.strIssue);
                if (lottery == null || lottery.Code == "")
                {
                    return;
                }

                // 开奖后重新初始化
                InitializeState();

                // 发送消息
                new Thread((ThreadStart)delegate
                {
                    if (OnOpen != null)
                    {
                        OnOpen(lottery, null);
                    }
                    
                }
                ).Start();
                
            }
        } 
    }
}
